package com.andrew.pegSolitareEngine;

import java.util.ArrayList;
import java.util.Iterator;

import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;


/*This class controls the game. It will use any options to request a new board.
 */

public class GameController {
	
	private BoardFactory bFactory;
	private Board aBoard;
	private final int SIZE_OF_BOARD = 5;  // SIZE_OF_BOARD refers the the length of one side
	
	private Button mNumberOfHoles;
	private Button mListOfHoles;
	private ImageView mBoard;
	private Context context;

	
	public GameController(Button mNumberOfHoles, Button mListOfHoles,
			ImageView mBoard, Context context) {
		this.mNumberOfHoles = mNumberOfHoles;
		this.mListOfHoles = mListOfHoles;
		this.mBoard = mBoard;
		this.context = context;
	}



	public void startGame(){
		bFactory = new BoardFactory();
		bFactory.createBoard(SIZE_OF_BOARD);
		aBoard = bFactory.getBoard();
		
		this.mNumberOfHoles.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				int duration = Toast.LENGTH_LONG;
				
				int numberOfHoles = aBoard.countHoles();
				String text = "Number of holes: " + numberOfHoles;	
										
				
				Toast toast = Toast.makeText(context, text, duration);
				toast.show();
			}
		});
		
		this.mListOfHoles.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				ArrayList<Hole> listOfHoles = aBoard.listOfHoles();
				Iterator<Hole> listIterator = listOfHoles.listIterator();
				
				String text = "Set of holes: {";
				if (listIterator.hasNext()){
					text = text + listIterator.next().getId();
				}
				
				while (listIterator.hasNext()){
					text = text + ", " + listIterator.next().getId();
				}		
				
				text = text + "}";
						
				int duration = Toast.LENGTH_LONG;
				
				Toast toast = Toast.makeText(context, text, duration);
				toast.show();
			}
		});
	}
}
